package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMakeOverBinding;
import com.sgb.kjwl.databinding.FragmentMakeOverDetailQuestionBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.sgb.kjwl.model.entity.enterprise.QuestionEnyiy;
import com.sgb.kjwl.model.entity.enterprise.TransferDataEntity;
import com.sgb.kjwl.model.entity.enterprise.TransferProcessEntity;
import com.sgb.kjwl.view.ui.activity.enterprise.QuestionDetailActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.QuestionView;
import com.sgb.kjwl.view.ui.adapter.enterprise.MakeOverDetailFlowAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.MakeOverDetailQuestionAdapter;
import com.sgb.kjwl.viewmodel.enterprise.MakeOverDetailQuestionModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MakeOverDetailQuestionFragment extends AppFragment {

    FragmentMakeOverDetailQuestionBinding mBinding;
    MakeOverDetailQuestionModel mViewModel;
    MakeOverDetailQuestionAdapter mAdapter;
    private final String[] mTitle = {"公司", "个人"};
    private List<TransferProcessEntity> mTransferProcessEntities = new ArrayList<>();
    private List<TransferDataEntity> mTransferData = new ArrayList<>();
    private MakeOverDetailFlowAdapter mFlowAdapter;
    private boolean flag = false;
    private StringBuilder mBuy = new StringBuilder(), mSale = new StringBuilder(), mProcess = new StringBuilder(), mGet = new StringBuilder();

    public static MakeOverDetailQuestionFragment newInstance(String type) {
        MakeOverDetailQuestionFragment makeOverDetailQuestionFragment = new MakeOverDetailQuestionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        makeOverDetailQuestionFragment.setArguments(bundle);
        return makeOverDetailQuestionFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_make_over_detail_question, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(MakeOverDetailQuestionModel.class);
        String type = getArguments().getString("type");

        initButton();
        initLiveData();
        initRecycle();
        initTabLayout();
        initData(type);
    }

    private void initData(String type) {
        mBinding.tvMakeoverDetailBuy.setText("/");
        mBinding.tvMakeoverDetailSale.setText("/");
        mBinding.tvMakeoverDetailTerrace.setText("/");
        mBinding.tvMakeoverDetailAcquire.setText("/");
        mViewModel.getCommonQuestion(type);
        mViewModel.getTransferData(type);
        mViewModel.getTransferProcess(type);
    }

    private void initTabLayout() {
        mBinding.tlMakeOverDetailDatum.addTab(mBinding.tlMakeOverDetailDatum.newTab().setText(mTitle[0]));
        mBinding.tlMakeOverDetailDatum.addTab(mBinding.tlMakeOverDetailDatum.newTab().setText(mTitle[1]));
        mBinding.tlMakeOverDetailDatum.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.tvMakeoverDetailBuy.setText("/");
                mBinding.tvMakeoverDetailSale.setText("/");
                mBinding.tvMakeoverDetailTerrace.setText("/");
                mBinding.tvMakeoverDetailAcquire.setText("/");
                mBuy.setLength(0);
                mSale.setLength(0);
                mProcess.setLength(0);
                mGet.setLength(0);
                if (tab.getPosition() == 0) {
                    for (TransferDataEntity transferDataEntity : mTransferData) {
                        if (transferDataEntity.getChangeIdentity() == 2) {
                            switch (transferDataEntity.getTransferDataType()) {
                                case 1:
                                    mSale.append("、").append(transferDataEntity.getProfileName());
                                    if (mSale.charAt(0) == '、') {
                                        mSale.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailSale.setText(TextUtils.isEmpty(mSale.toString()) ? "/" : mSale.toString());
                                    break;
                                case 2:
                                    mBuy.append("、").append(transferDataEntity.getProfileName());
                                    if (mBuy.charAt(0) == '、') {
                                        mBuy.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailBuy.setText(TextUtils.isEmpty(mBuy.toString()) ? "/" : mBuy.toString());
                                    break;
                            }
                        } else {

                            switch (transferDataEntity.getTransferDataType()) {
                                case 3:
                                    mProcess.append("、").append(transferDataEntity.getProfileName());
                                    if (mProcess.charAt(0) == '、') {
                                        mProcess.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailTerrace.setText(TextUtils.isEmpty(mProcess.toString()) ? "/" : mProcess.toString());
                                    break;
                                case 4:
                                    mGet.append("、").append(transferDataEntity.getProfileName());
                                    if (mGet.charAt(0) == '、') {
                                        mGet.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailAcquire.setText(TextUtils.isEmpty(mGet.toString()) ? "/" : mGet.toString());
                                    break;
                            }
                        }
                    }
                } else {
                    for (TransferDataEntity transferDataEntity : mTransferData) {
                        if (transferDataEntity.getChangeIdentity() == 1) {
                            switch (transferDataEntity.getTransferDataType()) {
                                case 1:
                                    mSale.append("、").append(transferDataEntity.getProfileName());
                                    if (mSale.charAt(0) == '、') {
                                        mSale.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailSale.setText(TextUtils.isEmpty(mSale.toString()) ? "/" : mSale.toString());
                                    break;
                                case 2:
                                    mBuy.append("、").append(transferDataEntity.getProfileName());
                                    if (mBuy.charAt(0) == '、') {
                                        mBuy.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailBuy.setText(TextUtils.isEmpty(mBuy.toString()) ? "/" : mBuy.toString());
                                    break;
                            }
                        } else {
                            switch (transferDataEntity.getTransferDataType()) {
                                case 3:
                                    mProcess.append("、").append(transferDataEntity.getProfileName());
                                    if (mProcess.charAt(0) == '、') {
                                        mProcess.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailTerrace.setText(TextUtils.isEmpty(mProcess.toString()) ? "/" : mProcess.toString());
                                    break;
                                case 4:
                                    mGet.append("、").append(transferDataEntity.getProfileName());
                                    if (mGet.charAt(0) == '、') {
                                        mGet.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailAcquire.setText(TextUtils.isEmpty(mGet.toString()) ? "/" : mGet.toString());
                                    break;
                            }
                        }
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initRecycle() {
        mBinding.rvMakeoverDetailQuestion.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new MakeOverDetailQuestionAdapter(getContext(), null);
        mBinding.rvMakeoverDetailQuestion.setAdapter(mAdapter);
        mAdapter.setmRecyclerClick(new RecyclerClick<QuestionEnyiy>() {
            @Override
            public void followClick(QuestionEnyiy entity) {
                QuestionDetailActivity.startactivity(getContext(), entity.getId() + "");
            }
        });
        mAdapter.setOnItemClick(new AdapterOnItemClick<QuestionEnyiy>() {
            @Override
            public void onItemClick(QuestionEnyiy questionEnyiy, int position) {
                QuestionDetailActivity.startactivity(getContext(), questionEnyiy.getId() + "");
            }
        });

        mBinding.rvMakeoverDetailFlow.setLayoutManager(new LinearLayoutManager(getContext()));
        mFlowAdapter = new MakeOverDetailFlowAdapter(getContext(), null);
        mBinding.rvMakeoverDetailFlow.setAdapter(mFlowAdapter);
    }

    private void initLiveData() {
        mViewModel.mQuestionList.observe(getViewLifecycleOwner(), questionEnyiys -> {
            if (questionEnyiys.size() > 0) {
                mAdapter.setDatas(questionEnyiys);
            } else {
                mBinding.llMakeoverDetailQuestion.setVisibility(View.GONE);
            }
        });

        mViewModel.mProcessList.observe(getViewLifecycleOwner(), new Observer<List<TransferProcessEntity>>() {
            @Override
            public void onChanged(List<TransferProcessEntity> transferProcessEntities) {
                if (transferProcessEntities.size() > 0) {
                    mTransferProcessEntities = transferProcessEntities;
                    if (transferProcessEntities.size() > 5) {
                        mBinding.tvMakeoverDetailEx.setVisibility(View.VISIBLE);
                        List<TransferProcessEntity> moreFive = new ArrayList<>();
                        for (int i = 0; i < 5; i++) {
                            moreFive.add(transferProcessEntities.get(i));
                        }
                        mFlowAdapter.setDatas(moreFive);
                    } else {
                        mBinding.llMakeoverDetailEx.setVisibility(View.GONE);
                        mFlowAdapter.setDatas(transferProcessEntities);
                    }
                } else {
                    mBinding.llMakeoverDetailFlow.setVisibility(View.GONE);
                }
            }
        });

        mViewModel.mDataList.observe(getViewLifecycleOwner(), new Observer<List<TransferDataEntity>>() {
            @Override
            public void onChanged(List<TransferDataEntity> transferDataEntities) {
                if (transferDataEntities.size() > 0) {
                    mBuy.setLength(0);
                    mSale.setLength(0);
                    mProcess.setLength(0);
                    mGet.setLength(0);
                    mTransferData = transferDataEntities;
                    for (TransferDataEntity transferDataEntity : mTransferData) {
                        if (transferDataEntity.getChangeIdentity() == 2) {
                            switch (transferDataEntity.getTransferDataType()) {
                                case 1:
                                    mSale.append("、").append(transferDataEntity.getProfileName());
                                    if (mSale.charAt(0) == '、') {
                                        mSale.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailSale.setText(TextUtils.isEmpty(mSale.toString()) ? "/" : mSale.toString());
                                    break;
                                case 2:
                                    mBuy.append("、").append(transferDataEntity.getProfileName());
                                    if (mBuy.charAt(0) == '、') {
                                        mBuy.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailBuy.setText(TextUtils.isEmpty(mBuy.toString()) ? "/" : mBuy.toString());
                                    break;
                            }
                        } else {
                            switch (transferDataEntity.getTransferDataType()) {
                                case 3:
                                    mProcess.append("、").append(transferDataEntity.getProfileName());
                                    if (mProcess.charAt(0) == '、') {
                                        mProcess.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailTerrace.setText(TextUtils.isEmpty(mProcess.toString()) ? "/" : mProcess.toString());
                                    break;
                                case 4:
                                    mGet.append("、").append(transferDataEntity.getProfileName());
                                    if (mGet.charAt(0) == '、') {
                                        mGet.delete(0, 1);
                                    }
                                    mBinding.tvMakeoverDetailAcquire.setText(TextUtils.isEmpty(mGet.toString()) ? "/" : mGet.toString());
                                    break;
                            }
                        }
                    }
                } else {
                    mBinding.llMakeoverDetailData.setVisibility(View.GONE);
                }
            }
        });

        mViewModel.mLoading.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.showProgressDialog(getContext(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false));
        mViewModel.mSuccess.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.dismissProgressDialog());
        mViewModel.mFail.observe(getViewLifecycleOwner(), aBoolean -> {
            DialogHelper.dismissProgressDialog();
            MToast.showToast(getContext(), "网络连接错误");
        });
    }

    private void initButton() {
        mBinding.llMakeoverDetailEx.setOnClickListener(this);
        mBinding.tvMakeoverDetailEx.setOnClickListener(this);
        mBinding.ivMakeoverDetailEx.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_makeover_detail_ex:
            case R.id.iv_makeover_detail_ex:
                if (flag) {
                    mFlowAdapter.setDatas(mTransferProcessEntities);
                    mBinding.tvMakeoverDetailEx.setText("收起");
                    mBinding.ivMakeoverDetailEx.setRotation(180);
                } else {
                    List<TransferProcessEntity> moreFive = new ArrayList<>();
                    for (int i = 0; i < 5; i++) {
                        moreFive.add(mTransferProcessEntities.get(i));
                    }
                    mFlowAdapter.setDatas(moreFive);
                    mBinding.tvMakeoverDetailEx.setText("展开");
                    mBinding.ivMakeoverDetailEx.setRotation(0);
                }

                flag = !flag;
                break;
        }
    }
}
